package com.kingdee.pda.ui.scan_outwarehouse;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.basecommon.utils.PreferencesUtils;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.ScanOutOrderInfoBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.util.CommonUtil;

/**
 * 扫描出库
 * Created by dlj
 * DATE 2021/9/15
 * TIME 10:20
 */
public class ScanOutWarehouseActivity extends BaseTitleActivity implements View.OnKeyListener, ScanOutWarehouseContract.IScanOutWarehouseView {
    private EditText et_orderNo;
    private TextView tv_outOrderNo, tv_logisticsNo, tv_skuNum, tv_skuWeight,tv_orderNum;
    private CheckBox cb_isConfirm;
    private Button btn_confirm;
    private RelativeLayout rl_orderInfo;
    private ScanOutOrderInfoBean orderInfoBean;
    private ScanOutWarehousePresenter presenter;
    private int orderNum = 0;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, ScanOutWarehouseActivity.class));
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {

    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_outwarehouse);

    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("扫描出库");

        et_orderNo = findViewById(R.id.et_orderNo);
        setEditTextScannerEvents(et_orderNo, this);

        tv_outOrderNo = findViewById(R.id.tv_outOrderNo);
        tv_logisticsNo = findViewById(R.id.tv_orderStatus);
        tv_skuNum = findViewById(R.id.tv_skuTypes);
        tv_skuWeight = findViewById(R.id.tv_skuNumber);
        cb_isConfirm = findViewById(R.id.cb_isConfirm);
        rl_orderInfo = findViewById(R.id.rl_orderInfo);
        btn_confirm = findViewById(R.id.btn_confirm);

        tv_orderNum = findViewById(R.id.tv_orderNum);

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scanCommit();
            }
        });

        cb_isConfirm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                PreferencesUtils.putBoolean(ScanOutWarehouseActivity.this, "cb_isConfirm", b);
            }
        });

        cb_isConfirm.setChecked(PreferencesUtils.getBoolean(ScanOutWarehouseActivity.this, "cb_isConfirm"));

        presenter = new ScanOutWarehousePresenter(this, this);
    }

    private void scanCommit() {
        String number = et_orderNo.getText().toString();
        if (orderInfoBean != null && !TextUtils.isEmpty(number)) {
            presenter.confirmStockOut(orderInfoBean.getId(), orderInfoBean.getFormId(), orderInfoBean.getNumberType(), number);
        } else {
            SpeakUtils.speakWithToast(ScanOutWarehouseActivity.this, "请先输入单号并确认！", true, false);
            CommonUtil.requestFocusAndClearText(et_orderNo);
        }
    }

    @Override
    protected void initData() {
        super.initData();

    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if (TextUtils.isEmpty(input)) return false;
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            if (v.getId() == R.id.et_orderNo) {
                presenter.scanStockOutBillOrExpressNo(input);
                return true;
            }
        }
        return false;
    }

    @Override
    public void scanStockOutBillOrExpressNoSuccess(ScanOutOrderInfoBean orderInfoBean) {
        SpeakUtils.onlyRing(this,2);
        if (getCurrentWarehouse().getId() != orderInfoBean.getWarehouseId()) {
            SpeakUtils.speakWithToast(ScanOutWarehouseActivity.this, "此单不属于该仓库！", true, false);
            CommonUtil.requestFocusAndClearText(et_orderNo);
            return;
        }
        this.orderInfoBean = orderInfoBean;
        rl_orderInfo.setVisibility(View.VISIBLE);
        btn_confirm.setEnabled(true);
        tv_logisticsNo.setText(orderInfoBean.getMailno());
        tv_outOrderNo.setText(orderInfoBean.getBillno());

        int allQty = 0;
        for (ScanOutOrderInfoBean.StockOutOrderDetailInfosBean bean : orderInfoBean.getStockOutOrderDetailInfos()) {
            allQty += bean.getQty();
        }
        tv_skuNum.setText(allQty + "");
        tv_skuWeight.setText(orderInfoBean.getPackageweight() + "kg");

        //如果自动提交
        if (cb_isConfirm.isChecked()) scanCommit();
    }

    @Override
    public void scanStockOutBillOrExpressNoFailed(String msg) {
        SpeakUtils.speakWithToast(this, msg, true, false);
        btn_confirm.setEnabled(false);
        rl_orderInfo.setVisibility(View.INVISIBLE);
        CommonUtil.requestFocusAndClearText(et_orderNo);
    }

    @Override
    public void confirmStockOutResult(boolean isSuccess, String msg) {
        if (isSuccess) {
            SpeakUtils.onlyRing(this,1);
            rl_orderInfo.setVisibility(View.INVISIBLE);
            orderInfoBean = null;
            orderNum++;
            tv_orderNum.setText(orderNum+"");
        } else {
            SpeakUtils.speakWithToast(this, msg, true, false);
        }
        btn_confirm.setEnabled(false);
        CommonUtil.requestFocusAndClearText(et_orderNo);
    }
}
